package com.live.dengdengtv.activity.support;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.kernel.avcontrollers.QavsdkControl;
import com.live.dengdengtv.kernel.presenters.Presenter;
import com.live.dengdengtv.kernel.presenters.viewinface.LoginView;
import com.live.dengdengtv.bean.Constants;
import com.live.dengdengtv.kernel.utils.LogConstants;
import com.live.dengdengtv.kernel.utils.SxbLog;
import com.tencent.TIMCallBack;
import com.tencent.TIMManager;
import com.tencent.TIMUser;

/**
 * 登录的数据处理类
 */
public class LoginHelper extends Presenter {
    private Context mContext;
    private static final String TAG = LoginHelper.class.getSimpleName();
    private LoginView mLoginView;

    private int RoomId = -1;

    public LoginHelper(Context context) {
        mContext = context;
    }

    public LoginHelper(Context context, LoginView loginView) {
        mContext = context;
        mLoginView = loginView;
    }


    /**
     * 登录imsdk
     *
     * @param identify 用户id
     * @param userSig  用户签名
     */
    public void imLogin(final String identify, String userSig) {

//        TIMManager.getInstance().addMessageListener(privateMsgLis);

        TIMUser user = new TIMUser();
        user.setAccountType(String.valueOf(Constants.ACCOUNT_TYPE));
        user.setAppIdAt3rd(String.valueOf(Constants.SDK_APPID));
        user.setIdentifier(identify);
        //发起登录请求
        TIMManager.getInstance().login(
                Constants.SDK_APPID,
                user,
                userSig,                    //用户帐号签名，由私钥加密获得，具体请参考文档
                new TIMCallBack() {
                    @Override
                    public void onError(int i, String s) {
                        Toast.makeText(mContext, "IMLogin fail ：" + i + " msg " + s, Toast.LENGTH_SHORT).show();
                        if (mLoginView != null) {
                            mLoginView.loginFail();
                        }
                    }

                    @Override
                    public void onSuccess() {
//                        Toast.makeText(mContext, "IMLogin succ !", Toast.LENGTH_SHORT).show();
                        SxbLog.d(TAG, LogConstants.ACTION_HOST_CREATE_ROOM + LogConstants.DIV + identify + LogConstants.DIV + "request room id");
//                        getMyRoomNum();
                        startAVSDK();
                    }
                });
    }

    /**
     * 初始化AVSDK
     */
    private void startAVSDK() {
        QavsdkControl.getInstance().setAvConfig(
                Constants.SDK_APPID, "" + Constants.ACCOUNT_TYPE,
                LoginInfo.getInstance().getUserId(),
                LoginInfo.getInstance().getUserSig());
        int result = QavsdkControl.getInstance().startContext();
        Log.e(TAG,"startAVSDK=="+result);
        if (mLoginView != null)
            mLoginView.loginSucc();
    }
    @Override
    public void onDestory() {
        mLoginView = null;
        mContext = null;
    }


    /**
     * 私信消息回调
     */
   /* public static TIMMessageListener privateMsgLis = new TIMMessageListener() {
        @Override
        public boolean onNewMessages(List<TIMMessage> list) {


            return false;
        }
    };*/
}
